<?php

class stdioADP {
	/**
	 *  @param size 文件 长度是否限制
	 */
	
	function write($file, $data, $append = FALSE, $size = - 1, $attr = array() , $compress = FALSE) {
		if (!file_exists($file)) {
			if (!$this->mkdir(dirname($file))) return FALSE;
		}
		$len = FALSE;
		$mode = $append ? 'ab' : 'wb';
		$fp = @fopen($file, $mode);
		if (!$fp) {
			LOGSTR('io', 'fopen file error,file:' . $file);
			exit("Can not open file $file !");
		}
		flock($fp, LOCK_EX);
		$len = @fwrite($fp, $data);
		flock($fp, LOCK_UN);
		@fclose($fp);
		return $len;
	}
	
	function read($file) {
		if (!file_exists($file)) {
			return FALSE;
		}
		if (!is_readable($file)) {
			LOGSTR('io', 'file can not be read,file:' . $file);
			return FALSE;
		}
		if (function_exists('file_get_contents')) {
			return file_get_contents($file);
		}
		else {
			return (($contents = file($file))) ? implode('', $contents) : FALSE;
		}
	}
	
	function fgets() {
	}
	/// get files and dirs not use recursion
	
	function ls($dir, $r = FALSE, $info = FALSE) {
		if (empty($dir)) $dir = '.';
		if (!file_exists($dir) || !is_dir($dir)) {
			return FALSE;
		}
		$fs = array();
		$ds = array(
			$dir
		);
		while (count($ds) > 0) {
			foreach ($ds as $i => $d) {
				unset($ds[$i]);
				$handle = opendir($d);
				while (FALSE !== ($item = readdir($handle))) {
					if ($item == '.' || $item == '..') continue;
					$fp = ($d == '.' || $d == '.\\' || $d == './') ? $item : $d . DIRECTORY_SEPARATOR . $item;
					$t = is_file($fp) ? 'f' : (is_dir($fp) ? 'd' : 'o');
					if (is_dir($fp) && $r) {
						$ds[] = $fp;
					}
					$fs[] = ($info ? array(
						$t,
						$fp,
						$this->info($fp)
					) : array(
						$t,
						$fp
					));
				}
			}
		}
		return $fs;
	}
	
	function mkdir($path) {
		$rst = TRUE;
		if (!file_exists($path)) {
			$this->mkdir(dirname($path));
			$rst = @mkdir($path, 0777);
		}
		return $rst;
	}
	
	function rm($path) {
		$path = rtrim($path, '/\\ ');
		if (!is_dir($path)) {
			return @unlink($path);
		}
		if (!$handle = opendir($path)) {
			LOGSTR('io', 'opendir error,dir:' . $path);
			return FALSE;
		}
		while (FALSE !== ($file = readdir($handle))) {
			if ($file == "." || $file == "..") continue;
			$file = $path . DIRECTORY_SEPARATOR . $file;
			if (is_dir($file)) {
				$this->rm($file);
			}
			else {
				if (!@unlink($file)) {
					LOGSTR('io', 'delete file error when delete dir,file:' . $file);
					return FALSE;
				}
			}
		}
		closedir($handle);
		if (!rmdir($path)) {
			LOGSTR('io', 'delete dir error,dir:' . $path);
			return FALSE;
		}
		return TRUE;
	}
	
	function info($path = ".", $key = FALSE) {
		$path = realpath($path);
		if (!$path) FALSE;
		$result = array(
			"name" => substr($path, strrpos($path, DIRECTORY_SEPARATOR) + 1) ,
			"location" => $path,
			"type" => is_file($path) ? 1 : (is_dir($path) ? 0 : -1) ,
			"size" => filesize($path) ,
			"access" => fileatime($path) ,
			"modify" => filemtime($path) ,
			"change" => filectime($path) ,
			"read" => is_readable($path) ,
			"write" => is_writable($path)
		);
		clearstatcache();
		return $key ? $result[$key] : $result;
	}
}
?>
